﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.ECommerceCatalog.User.Views;
using WCSFContrib.ECommerceCatalog.User.Tests.Mocks;
using WCSFContrib.ECommerceCatalog.BusinessEntities;

namespace WCSFContrib.ECommerceCatalog.User.Tests
{
    /// <summary>
    /// Summary description for CartPresenterTestFixture
    /// </summary>
    [TestClass]
    public class CartPresenterTestFixture
    {
        private MockCartView _view;
        private MockUserController _controller;

        public CartPresenterTestFixture()
        {
        }

        private CartPresenter GetCartPresenterInitialized()
        {
            _controller = new MockUserController();
            _view = new MockCartView();
            CartPresenter presenter = new CartPresenter(_controller);
            presenter.View = _view;

            return presenter;
        }

        [TestMethod]
        public void ShouldShowCartOnViewInitializedWhenCartIsNotNull()
        {
            CartPresenter presenter = GetCartPresenterInitialized();

            presenter.UserName = "userTest";
            presenter.OnViewInitialized();

            Assert.IsTrue(_controller.StartUserPageFlowCalled);
            Assert.IsTrue(_controller.GetCartByUserNameCalled);
            Assert.IsTrue(_controller.CurrentPageCalled);
            Assert.IsTrue(_view.ShowCartCalled);
        }

        [TestMethod]
        public void ShouldShowCartOnViewInitializedWhenCartIsNull()
        {
            CartPresenter presenter = GetCartPresenterInitialized();

            presenter.UserName = "unknownUser";
            presenter.OnViewInitialized();

            Assert.IsTrue(_controller.StartUserPageFlowCalled);
            Assert.IsTrue(_controller.GetCartByUserNameCalled);
            Assert.IsTrue(_controller.CurrentPageCalled);
            Assert.IsTrue(_view.ShowCartCalled);
        }

        [TestMethod]
        public void ShouldGetProductDetails()
        {
            CartPresenter presenter = GetCartPresenterInitialized();

            presenter.OnProductDetailsRequested(0);

            Assert.IsTrue(_controller.GetProductCalled);
            Assert.IsTrue(_controller.CurrentPageCalled);
            Assert.IsTrue(_view.ShowProductDetailsCalled);
        }

        [TestMethod]
        public void ShouldCheckoutOrder()
        {
            CartPresenter presenter = GetCartPresenterInitialized();

            presenter.OnCheckoutOrder();

            Assert.IsTrue(_controller.GoCheckoutPageCalled);
            Assert.IsTrue(_controller.CurrentPageCalled);
        }

        [TestMethod]
        public void ShouldDeleteOrder()
        {
            CartPresenter presenter = GetCartPresenterInitialized();
            string userName = "userTest";

            presenter.OnDeleteProduct(userName, 0, 9);

            Assert.IsTrue(_controller.RemoveItemFromCartCalled);
            Assert.IsTrue(_controller.GetCartByUserNameCalled);
            Assert.IsTrue(_controller.CurrentPageCalled);
            Assert.IsTrue(_view.ShowCartCalled);
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion
    }

    class MockCartView : ICartView
    {
        public bool ShowCartCalled = false;
        public bool ShowProductDetailsCalled = false;

        #region ICartView Members

        public void ShowCart(Cart cart)
        {
            ShowCartCalled = true;
        }

        public void ShowProductDetails(Item item)
        {
            ShowProductDetailsCalled = true;
        }

        #endregion
    }
}

